package com.micmac.main.modules.services {
	import com.AppConst;
	import com.micmac.main.events.RegisterEvent;
	import com.micmac.main.modules.events.CustomModuleEvent;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	import mx.core.IVisualElement;
	import mx.core.IVisualElementContainer;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.ModuleEvent;
	import mx.managers.CursorManager;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	import mx.skins.halo.BusyCursor;
	
	import org.robotlegs.mvcs.Actor;
	
	import spark.modules.Module;
	
	public class ModuleManagerService extends Actor {
		
		private var _moduleInfo:IModuleInfo;
		
		
		
		public function ModuleManagerService() {
			super();
		}
		
		public function loadModule($url:String):void {
			trace("[ModuleManagerService.loadModule]");
			_moduleInfo = ModuleManager.getModule($url);
			addLoadModuleEventHandlers(_moduleInfo);
			_moduleInfo.load();
			AppConst.setAppBusyCursor();
			//If you uncomment this line context listeners will work correctly
			/*var regEvent:RegisterEvent = new RegisterEvent(RegisterEvent.REGISTER_CUSTOMER_CONTROLL);
			dispatch(regEvent);*/
		}
		
		
		/**
		 * Creates and returns an instance of a module with $url if it is ready
		 */
		public function createModuleInstance($url:String, $parent:IVisualElementContainer = null):Module {
			
			var moduleInstance:Module = null;
			
			var moduleInfo:IModuleInfo = ModuleManager.getModule($url)
			if(moduleInfo.loaded && _moduleInfo.factory) {
				moduleInstance = _moduleInfo.factory.create() as Module;
				if($parent) {
					$parent.addElement(moduleInstance as IVisualElement);
					moduleInstance.addEventListener(FlexEvent.CREATION_COMPLETE,onCreationComplete);
				}
			}
			
			return moduleInstance;
		}
		
	/*	
		private function onComplete(event:FlexEvent):void
		{
			var regEvent:RegisterEvent = new RegisterEvent(RegisterEvent.REGISTER_CUSTOMER_CONTROLL);
			dispatch(regEvent);
		}
		*/
		
		private function addLoadModuleEventHandlers($moduleInfo:IModuleInfo):void {
			$moduleInfo.addEventListener(ModuleEvent.READY, onModuleLoadReadyHandler);
			$moduleInfo.addEventListener(ModuleEvent.ERROR, onModuleLoadErrorHandler);
			$moduleInfo.addEventListener(ModuleEvent.PROGRESS,onProgressData);
			//$moduleInfo.addEventListener(FlexEvent.CREATION_COMPLETE,onCreationComplete);
		}
		
		private function removeLoadModuleEventHandlers($moduleInfo:IModuleInfo):void {
			$moduleInfo.removeEventListener(ModuleEvent.READY, onModuleLoadReadyHandler);
			$moduleInfo.removeEventListener(ModuleEvent.ERROR, onModuleLoadErrorHandler);
		}
		
		private function onCreationComplete(e:FlexEvent):void
		{
			AppConst.removeAppBusyCursor();
		}
		
		private function onProgressData(event:ModuleEvent):void
		{
			var totalBytes:Number = event.bytesTotal;
			var loadedBytes:Number = event.bytesLoaded;
			trace(totalBytes/loadedBytes);
			
		}

		private function onModuleLoadReadyHandler(event:ModuleEvent):void {
			trace("[ModuleManagerService.onModuleLoadReadyHandler]");
			
			var moduleUrl:String = event.module.url;
			removeLoadModuleEventHandlers(event.module);
			//AppConst.removeAppBusyCursor();
			
			//dispatch CustomModuleEvent.LOADED
			var moduleEvent:CustomModuleEvent = new CustomModuleEvent(CustomModuleEvent.LOADED);
			moduleEvent.url = moduleUrl;
			
			dispatch(moduleEvent);
		}
		
		private function onModuleLoadErrorHandler(event:ModuleEvent):void {
			trace("[ModuleManagerService.onModuleLoadErrorHandler]");
			
			var moduleUrl:String = event.module.url;
			removeLoadModuleEventHandlers(event.module);
			
			//dispatch CustomModuleEvent.ERROR
			var moduleEvent:CustomModuleEvent = new CustomModuleEvent(CustomModuleEvent.ERROR);
			moduleEvent.url = moduleUrl;
			
			dispatch(moduleEvent);
		}
	}
}